package com.generic.mybatis.dal.interceptor.automap.builder;

import com.generic.mybatis.dal.interceptor.automap.SqlBuilder;
import com.generic.mybatis.dal.interceptor.automap.TableMapper;
import com.generic.mybatis.dal.interceptor.automap.TableMapperHolder;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.mapping.MappedStatement;

import static com.generic.mybatis.dal.interceptor.automap.builder.SqlBuilderUtil.buildIdWhereStatement;

public class DeleteByIdBuilder implements SqlBuilder {
    @Override
    public String buildSql(MappedStatement mappedStatement, Class dtoClass, Object parameterObject) throws Exception {
        if (null == parameterObject) {
            throw new RuntimeException("Sorry,I refuse to build sql for a null object!");
        }
        TableMapper tableMapper = TableMapperHolder.getTableMapper(dtoClass);
//        Object dtoFieldMap = null;
//        if (!parameterObject.getClass().isPrimitive() && !(parameterObject instanceof String)) {
//            dtoFieldMap = PropertyUtils.describe(parameterObject);
//        }else{
//            dtoFieldMap = parameterObject;
//        }


        SQL sql = new SQL().UPDATE(String.format("%s as %s", tableMapper.getTableName(), tableMapper.getAlias()));
        //sql.SET("is_deleted = 'y'");
        sql.SET("is_deleted = '1'");

        // delete from tableName where primaryKeyName=?
        buildIdWhereStatement(sql, tableMapper, parameterObject);
        return sql.toString();
    }
}
